package com.XinYun.Library.modules.borrow.Service;
import com.XinYun.Library.modules.borrow.Dao.*;
import com.XinYun.Library.utils.SqlUtils.DruidUtil;

import java.sql.Date;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


public class ServiceImpl implements Service{
    @Override
    public ResultSet isbanned(Object obj) {
        String sql="select isbanned from user_info where uid = ?";
        borrowDao bD=new borrowDaoImpl();
        try {
            ResultSet times=bD.select(sql,obj);
            return times;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<String> endtime(Object obj) {
        String sql="select endtime from user_loan where uid = ?";
        borrowDaoImpl bD=new borrowDaoImpl();
        List<String> list=new ArrayList<>();
        try {
            ResultSet rs=bD.select(sql,obj);
            Date time1=new Date(rs.getTimestamp("endtime").getTime());//java.util.Date
            //注意，是从时间戳中再得到时间，直接getDate不可取
            SimpleDateFormat formattime = new SimpleDateFormat("yyyy-MM-dd");
            //时间格式化
            String pubtime=formattime.format(time1);
            list.add(pubtime);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DruidUtil.close();
        }
        return list;
    }

    @Override
    public ResultSet bookState(Object obj) {
        String sql="select state from book_id where bid = ?";
        borrowDao bD=new borrowDaoImpl();
        try {
            ResultSet Rs=bD.select(sql,obj);
            return Rs;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int State1(Object obj) {
        String sql="update book_id set state = 1 where bid = ?";
        borrowDao bD=new borrowDaoImpl();
        try {
            int line=bD.update(sql,obj);
            DruidUtil.close();
            return line;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int State0(Object obj) {
        String sql="update book_id set state = 0 where bid = ?";
        borrowDao bD=new borrowDaoImpl();
        try {
            int line=bD.update(sql,obj);
            DruidUtil.close();
            return line;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int State_1(Object obj) {
        String sql="update book_id set state = -1 where bid = ?";
        borrowDao bD=new borrowDaoImpl();
        try {
            int line=bD.update(sql,obj);
            DruidUtil.close();
            return line;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int late(Object obj) {
        borrowDao dao=new borrowDaoImpl();
        List<String> list=endtime(obj);
        int row = list.size();
        String date=null;
        if(row == 0) {
            list.add("");
        }
        if(list.get(0) == "")
        {
           return -1;
        }
        else {
            date=list.get(0);
        }
        String day=dao.day(date);
        if(Integer.parseInt(day)<=1)
        return 1;
        else return 0;
    }
    public int late1(Object obj) {
        borrowDao dao=new borrowDaoImpl();
        List<String> list=endtime(obj);
        int row = list.size();
        String date=null;
        if(row == 0) {
            list.add("");
        }
        if(list.get(0) == "")
        {
            return -1;
        }
        else {
            date=list.get(0);
        };
        String day=dao.day(date);
        if(Integer.parseInt(day)<=0)
            return 1;
        else return 0;
    }

    @Override
    public int addBlack(Object obj) {
        String sql="update user_info set isbanned = 1 where uid = ? , times >= 3";
        borrowDao bD=new borrowDaoImpl();
        try {
            int line=bD.update(sql,obj);
            return line;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
